package com.haiqi.modules.ship.web;

import com.github.pagehelper.PageInfo;
import com.haiqi.common.BaseController;
import com.haiqi.common.annotation.SafeAuthority;
import com.haiqi.common.bean.DataGridBean;
import com.haiqi.common.bean.PageBean;
import com.haiqi.common.persistence.entity.*;
import com.haiqi.common.utils.PageUtils;
import com.haiqi.modules.main.bean.ResponseBean;
import com.haiqi.modules.ship.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by liuxiayan on 2017/6/6.
 */

@Controller
@RequestMapping("/ship/guanzhu")
@SafeAuthority(menuCode = "monitorLocation,companyMonitorLocation")
public class ShipGuanZhuController  extends BaseController {

    private String menuCode = "shipfocus";

    @Autowired
    private GuanZhuService guanZhuService;
    @Autowired
    private ShipUserService shipUserService;
    @Autowired
    private AisShipStaticService aisShipStaticService;
    @Autowired
    private AisShipDynaService aisShipDynaService;
    @Autowired
    private ShipFollowService shipFollowService;
    @RequestMapping("/select")
    public String select(HttpSession session,Model model){
        ResponseBean.UserBean user = getUser(session);
        if(user == null){
            return "/modules/user/login";
        }
        GuanZhu guanZhu=new GuanZhu();
        guanZhu.setCompanyId(user.getOfficeid());
        List<GuanZhu> guanZhuList = guanZhuService.select(guanZhu);
        model.addAttribute("guanZhuList",guanZhuList);
        return "/modules/map/window/myfleet";
    }



    @RequestMapping("/list")
    public String list(HttpSession session,Model model, PageBean pageBean,ShipFollow shipFollow,String keyword){
        ResponseBean.UserBean user = getUser(session);
        if(user == null){
            return "/modules/user/login";
        }
        GuanZhu guanZhu=new GuanZhu();
        guanZhu.setCompanyId(user.getOfficeid());
        shipFollow.setMmsi(keyword);
        shipFollow.setShipname(keyword);
        shipFollow.setGuanZhu(guanZhu);
        generateMenuCode(session, menuCode);
        List<GuanZhu>  guanZhuList=  guanZhuService.select(guanZhu);
        PageInfo<ShipFollow> pageInfo=shipFollowService.select(shipFollow,pageBean);
        model.addAttribute("guanZhuList",guanZhuList);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("param",shipFollow);
        model.addAttribute("keyword",keyword);
        return "/modules/ship/followShipList";
    }

    @ResponseBody
    @RequestMapping("/ajax/groupList")
    public List<GuanZhu> getGroupListByCompany(HttpSession session){
        ResponseBean.UserBean user = getUser(session);
        GuanZhu guanZhu=new GuanZhu();
        guanZhu.setCompanyId(user.getOfficeid());
        List<GuanZhu>  guanZhus=  guanZhuService.select(guanZhu);
        return guanZhus;
    }

    @RequestMapping("/manageGroup")
    public String manageGroup(HttpSession session,Model model,GuanZhu guanZhu){
        ResponseBean.UserBean user = getUser(session);
        guanZhu.setCompanyId(user.getOfficeid());
        List<GuanZhu>  guanZhuList=  guanZhuService.select(guanZhu);
        model.addAttribute("guanZhuList",guanZhuList);
        return "/modules/ship/manageGroup";
    }

    @RequestMapping("/add")
    public String addFollowShip(){

        return "/modules/ship/addFollowShip";
    }



    @RequestMapping("/insertGZ")
    @ResponseBody
    public boolean insertGZ(String gzname,HttpSession session){
        ResponseBean.UserBean user = getUser(session);
        GuanZhu guanZhu = new GuanZhu();
        if(gzname == null){
          return false;
        }
        GuanZhu G = guanZhuService.findByName(gzname);
        if(G != null){
            return false;
        }
        guanZhu.setName(gzname);
        guanZhu.setCompanyId(user.getOfficeid());
        return guanZhuService.insertGZ(guanZhu);
    }
    @RequestMapping("/deleteGZ")
    @ResponseBody
    public boolean deleteGZ(String gzid){
        List<ShipFollow> shipFollowList = shipFollowService.findByGuanId(Integer.parseInt(gzid));
        if(shipFollowList.size() > 0 ){
            boolean flag = shipFollowService.deleteGZ(Integer.parseInt(gzid));
            if(flag == false){
                return false;
            }else{
                return guanZhuService.deleteGZ(Integer.parseInt(gzid));
            }
        }else{
            return guanZhuService.deleteGZ(Integer.parseInt(gzid));
        }
    }
    @RequestMapping("/myownFleet")
    @ResponseBody
    public DataGridBean<CrewUser> getAjaxList(String shipName, String searchType, PageBean pageBean, HttpSession session){
        PageUtils.generatePage(pageBean);
        ResponseBean.UserBean user = getUser(session);
        if(user == null){
            return null;
        }
        FleetInfo fleetInfo = null;
        List<CrewUser> list = null;
        list = shipUserService.getListByCompanyId(user.getOfficeid(),"1",shipName);
        PageInfo<CrewUser> pageInfo = new PageInfo<>(list);
        DataGridBean<CrewUser> gridBean = new DataGridBean<>();
        PageUtils.convertToEasyuiList(pageInfo,gridBean);
        return gridBean;
    }
    @ResponseBody
    @RequestMapping("/getotherShip")
    public DataGridBean<AisShipStatic> getOtherShipList(String shipName,PageBean pageBean,HttpSession session) {
        //System.out.println();
        PageUtils.generatePage(pageBean);
        //ResponseBean.UserBean user = getUser(session);
        List<AisShipDyna> list = aisShipDynaService.getListByShipNameOrMmsi(shipName);
        PageInfo<AisShipDyna> pageInfo = new PageInfo<>(list);
        DataGridBean<AisShipStatic> gridBean = new DataGridBean<>();
        PageUtils.convertToEasyuiList(pageInfo,gridBean);
        return gridBean;
    }
    @RequestMapping("/addGuanZhu")
    public String addGuanZhu(Model model,HttpSession session){
        ResponseBean.UserBean user = getUser(session);
        if(user == null){
            return null;
        }
        return "/modules/map/window/addGuanZhu";
    }
    @RequestMapping("/addGuan")
    public String addGuan(Model model,HttpSession session){
        return "/modules/map/window/addGuan";
    }


    @RequestMapping("/ownFleet")
    public  String ownFleet(Model model,HttpSession session){
//        ResponseBean.UserBean user = getUser(session);
//        model.addAttribute("company",user.getOfficeid());
        return "/modules/map/window/ownFleet";}


    @RequestMapping("/lieBiao")
    public String lieBiao(HttpSession session,Integer id,Model model){
        ResponseBean.UserBean user = getUser(session);
           session.setAttribute("guanid",id);
           return "/modules/map/window/liebiao";
    }
    @RequestMapping("/fleetManager")
    public String fleetManager(HttpSession session, Model model){
        ResponseBean.UserBean user = getUser(session);
        model.addAttribute("company",user.getOfficeid());
        return "/modules/map/window/fleetManager";
    }
    @RequestMapping("/delGuan")
    public String delGuan(Model model,HttpSession session){
        return "/modules/map/window/delGuan";}
    @ResponseBody
    @RequestMapping("/guanZhuLiebiao")
    public  DataGridBean<ShipFollow> selectFollow(Integer id, PageBean pageBean) {
        PageUtils.generatePage(pageBean);
        List<ShipFollow> shipFollowList = shipFollowService.findByGuanId(id);
        PageInfo<ShipFollow> pageInfo = new PageInfo<>(shipFollowList);
        DataGridBean<ShipFollow> gridBean = new DataGridBean<>();
        PageUtils.convertToEasyuiList(pageInfo,gridBean);
        return gridBean;
    }
    @RequestMapping("/dropGuanZhu")
    @ResponseBody
    public boolean dropGuanZhu(String sfid){
        ShipFollow shipFollow =  shipFollowService.selectFollow(Integer.parseInt(sfid));
        if(shipFollow == null){
            return false;
        }
        return shipFollowService.deleteById(Integer.parseInt(sfid));
    }

    @ResponseBody
    @RequestMapping("/findByMmsi")
    public Map findByMmsi(String[] mmsi){
        System.out.println("================"+mmsi);
        Map map = new HashMap();
//        ShipFollow shipFollow = shipFollowService.findByMmsi(mmsi);
//        map.put("result",shipFollow);
        return map;
    }
    @RequestMapping("/trail")
    public String trail(String iemi,String start,String end,HttpSession session){
        TrailBean trailBean = new TrailBean();
        trailBean.setIemi(iemi);
        trailBean.setStartTime(start);
        trailBean.setEndTime(end);
        System.out.printf("============="+trailBean);
        session.setAttribute("trail",trailBean);
        return "/modules/map/window/trail";
    }
}
